﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NETMovie.Entities;
using NETMovie.UI.WebMVC.Models;
using NETMovie.BLL;
using NETMovie.UI.WebMVC.Mappers;

namespace NETMovie.UI.WebMVC.Controllers
{
    public class MovieStreamController : Controller
    {
        //
        // GET: /MovieStream/

        public ActionResult Index()
        {
            return View();
        }


        //
        // GET: /Movie/

        public ActionResult BrowseMovie()
        {
            SearchMovieViewModel s = new SearchMovieViewModel();
            s.Results = new List<MovieModel>();
            MovieService service = new MovieService();
            MovieCategory[] movieCategoryList = service.GetCategories();
            Session["Category"] = new MovieMapper().MapMovieCategories(movieCategoryList);

            return View(s);
        }

        [HttpPost]
        public ActionResult BrowseMovie(SearchMovieViewModel s, string SearchMoviePageButton)
        {
            MovieMapper mapper = new MovieMapper();

         
                // SearchMovieResultRecord searchRecordModel = new SearchMovieResultRecord();

               
                IEnumerable<Movie> movieList;
                    MovieService service = new MovieService();

            //last parameter is for whether to consider delete flag
            movieList = service.GetMovies(s.Title, s.Category, s.Keyword, false);
                  

                    if (movieList == null) { movieList = new List<Movie>(); }
                    s.Results = mapper.ReMapSearchMovie(movieList);


                    return View(s);
         
        }

        public ActionResult ViewMovieDetails(int id)
        {
            MovieModel moviemodel = new MovieModel();

            MovieService service = new MovieService();

            NETMovie.Entities.Movie movie = service.GetMovie(id);
            //   bool isRented = service.IsRented(id);

            if (movie == null)
            {

                return RedirectToAction("SearchMovie", "Home");
            }

            MovieMapper mapper = new MovieMapper();


            moviemodel = mapper.ReMapDetail(movie);

            return View(moviemodel);

        }

       [HttpPost]
        public ActionResult ViewMovieDetails(MovieModel model)
        {
           
            new RentalController().RentMovie(model.MovieId);
            HandleErrors();
            if (ModelState.IsValid)
            {

                model.IsRented = true;
            }

            return View(model);

        }
  

        private void HandleErrors()
        {
            List<string> errors = TempData["Errors"] as List<string>;
            if (errors == null)
                return;

            AddErrors(errors);
        }

        private void AddErrors(List<string> errors)
        {
            foreach (string error in errors)
            {
                ModelState.AddModelError(error, error);
            }
        }
    }

}
